package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.dao.FamilyDao;
import com.gxuwz.medical.dao.PersonDao;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.exception.PersonNotFoundException;
import com.gxuwz.medical.tools.DateUtil;
/**
 * 家庭档案管理控制
 * @author 演示
 *
 */
public class FamilyServlet extends BaseServlet {
    private FamilyDao familyDao=new FamilyDao();
    private PersonDao personDao=new PersonDao();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m = req.getParameter("m");// 请求处理动作类型:list:显示列表；get：根据ID读取记录;input:新增数据;add:保存新记录；edit：更新记录
		String url = ERROR_PATH;
		try {
			if ("list".equals(m)) {	
				url="/page/family/family_list.jsp";
			} else if ("input".equals(m)) {
				url="/page/family/family_add.jsp";
			} else if ("get".equals(m)) {
				url="/page/family/family_edit.jsp";
			} else if ("member".equals(m)) {
				url="/page/family/family_addMember.jsp";
			}else if ("add".equals(m)) {
				url = this.add(req, resp);
			}else if ("addMember".equals(m)) {
				url = this.addMember(req, resp);
			}else if ("edit".equals(m)) {
				url = this.edit(req, resp);
			}else if ("editMember".equals(m)) {
				url = this.editMember(req, resp);
			}else if ("del".equals(m)) {
				url = this.del(req, resp);
			}else if ("delMember".equals(m)) {
				url = this.delMember(req, resp);
			}  
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		  process(req, resp, url);

	}
	
	private String add(HttpServletRequest req,HttpServletResponse resp)throws Exception{
		 //1:获取档案信息
		System.out.println("familyservlet的add方法");
		 String townNo=req.getParameter("town");
		 String villageNo=req.getParameter("village");
		 String groupNo=req.getParameter("group");
		 String housePro=req.getParameter("housePro");
		 //从session对象中获取当前用户名称
		 String creator=(String)req.getSession().getAttribute("userName");
		 //1.1实例化新家庭档案对象
		 Family family =new Family();
		 //将请求参数依次赋值给实例的成员属性
		 Area town=new Area(townNo);
		 Area village=new Area(villageNo);
		 Area group=new Area(groupNo);
		 family.setTown(town);
		 family.setVillage(village);
		 family.setGroupNo(group);
		 family.setHousePro(housePro);
		 family.setCreator(creator);
		 //家庭住址：镇名称+村名称+组名称
		 family.setAddress(town.getAreaname()+village.getAreaname()+group.getAreaname());
		 //2:获取户主个人信息
		 String cardID=req.getParameter("cardID");
		 String persname=req.getParameter("persname");
		 String sex=req.getParameter("sex");
		 String birthday=req.getParameter("birthday");
		 String persPro=req.getParameter("persPro");
		 String isRural=req.getParameter("isRural");
		 String occupation=req.getParameter("occupation");
		 String workUnit=req.getParameter("workUnit");
		 String liveAddress=req.getParameter("liveAddress");
		 String telephone=req.getParameter("telephone");
		 
		 //2.1实例化户主个人档案对象
		 Person holder=new Person();
		 //将户主农民档案信息依次赋值给户主实例。
		 holder.setCardID(cardID);
		 holder.setPersname(persname);
		 holder.setSex(sex);	 
		 holder.setBirthday(birthday);
		 //调用日期组件计算年龄
		 holder.setAge(DateUtil.diffYear(birthday));
		 holder.setPersPro(persPro);
		 holder.setRural("1".equals(isRural)?true:false);
		 holder.setOccupation(occupation);
		 holder.setWorkUnit(workUnit);
		 holder.setLiveAddress(liveAddress);
		 holder.setTelephone(telephone);
		 holder.setRelation("1");//户主
		 family.setHolder(holder);
		 //保存家庭档案信息
		 family.addFamily();
		 return "/page/family/family_list.jsp";
		
	}	
	private String addMember(HttpServletRequest req,HttpServletResponse resp)throws Exception{
		 //1:获取档案信息
		    String famicode=req.getParameter("famicode");
		 //从session对象中获取当前用户名称
		 String creator=(String)req.getSession().getAttribute("userName");
		 //1.1实例化新家庭档案对象
		 Family family =new Family();
		 family.setFamicode(famicode);
		 //2:获取户主个人信息
		 String cardID=req.getParameter("cardID");
		 String persname=req.getParameter("persname");
		 String sex=req.getParameter("sex");
		 String birthday=req.getParameter("birthday");
		 String persPro=req.getParameter("persPro");
		 String isRural=req.getParameter("isRural");
		 String occupation=req.getParameter("occupation");
		 String workUnit=req.getParameter("workUnit");
		 String liveAddress=req.getParameter("liveAddress");
		 String telephone=req.getParameter("telephone");
		 String relation =req.getParameter("relation");
		 
		 
		 //2.1实例化户主个人档案对象
		 Person member=new Person();
		 member.setCardID(cardID);
		 member.setPersname(persname);
		 member.setSex(sex);	 
		 member.setBirthday(birthday);
		 //调用日期组件计算年龄
		 member.setAge(DateUtil.diffYear(birthday));
		 member.setPersPro(persPro);
		 member.setRural("1".equals(isRural)?true:false);
		 member.setOccupation(occupation);
		 member.setWorkUnit(workUnit);
		 member.setLiveAddress(liveAddress);
		 member.setTelephone(telephone);
		 member.setRelation(relation);
		 member.setFamily(family);
		 int agriNum=member.isRural()?1:0;
		 //保存家庭成员信息
		 member.addMember(agriNum);
		 return "/page/family/family_list.jsp";
		
	}	
	private String edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
		Object[] params= new Object[5];
		params[0]=request.getParameter("town");
		params[1]=request.getParameter("village");
		params[2]=request.getParameter("group");
		params[3]=request.getParameter("housePro");
		params[4]=request.getParameter("famicode");
		try{
		this.familyDao.editOrdelObject("update t_family set town=?,village=?,groupNo=?,housePro=? where famicode=?", params);
		params=new Object[12];
		params[0]=request.getParameter("cardID");
		params[1]=request.getParameter("persname");
		params[2]=request.getParameter("sex");
		params[3]=request.getParameter("birthday");
		params[4]=request.getParameter("persPro");
		params[5]=Integer.valueOf(request.getParameter("isRural"));
		params[6]=request.getParameter("occupation");
		params[7]=request.getParameter("workUnit");
		params[8]=request.getParameter("liveAddress");
		params[9]=request.getParameter("telephone");
		params[10]=DateUtil.diffYear(request.getParameter("birthday"));
		params[11]=request.getParameter("perscode");
		System.out.println("workUnit="+params[7]+"liveAddress="+params[8]);
		this.personDao.editOrdelObject("update t_person set cardID=?,persname=?,sex=?,birthday=?,persPro=?,isRural=?,occupation=?,workUnit=?,liveAddress=?,telephone=?,age=? where perscode=?", params);
		return "/page/family/family_list.jsp";
		}catch(Exception e){
			request.setAttribute("error", "家庭信息修改有误");
			e.printStackTrace();
			return "/error.jsp";
		}	
	}
	private String editMember(HttpServletRequest request, HttpServletResponse response) {
		Object[] params;
		params=new Object[12];
		params[0]=request.getParameter("cardID");
		params[1]=request.getParameter("persname");
		params[2]=request.getParameter("sex");
		params[3]=request.getParameter("birthday");
		params[4]=request.getParameter("persPro");
		params[5]=Integer.valueOf(request.getParameter("isRural"));
		params[6]=request.getParameter("occupation");
		params[7]=request.getParameter("workUnit");
		params[8]=request.getParameter("liveAddress");
		params[9]=request.getParameter("telephone");
		params[10]=DateUtil.diffYear(request.getParameter("birthday"));
		params[11]=request.getParameter("id");
		System.out.println("workUnit="+params[7]+"liveAddress="+params[8]);
		try {
			this.personDao.editOrdelObject("update t_person set cardID=?,persname=?,sex=?,birthday=?,persPro=?,isRural=?,occupation=?,workUnit=?,liveAddress=?,telephone=?,age=? where perscode=?", params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/page/person/person_list.jsp";
	}
    private String del(HttpServletRequest request, HttpServletResponse response) throws PersonNotFoundException, SQLException {
    	Object[] params= new Object[1];
    	params[0]=request.getParameter("famicode");
		//2:调用方法
		try{
			this.familyDao.editOrdelObject("delete from t_family where famicode=?", params);
			this.personDao.editOrdelObject("delete from t_person where famicode=?", params);
			return "/page/family/family_list.jsp";
		}catch(Exception e){
			request.setAttribute("error", "服务端错误");
			
			e.printStackTrace();
			return "/error.jsp";
		}	
	}
    private String delMember(HttpServletRequest request, HttpServletResponse response) {
    	Object[] params= new Object[1];
    	System.out.println("delMember");
    	params[0]=request.getParameter("id");
    	try {
			this.personDao.editOrdelObject("delete from t_person where perscode=?", params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/page/person/person_list.jsp";
    	
    }
}
